﻿using FittyWars.BordUtils;
using FittyWars.MathUtil;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FittyWars
{
    public class Bord
    {
        #region Members
        private GameManager m_manager;
        private Tile m_mapRoot;
        private List<Element> m_elementsList;
        private List<Element> m_deadElementList;
        #endregion

        #region Properties
        public int Turn { get; set; }
        public double BordWidth { get; private set; }
        public double BordHeight { get; private set; }
        #endregion

        #region Ctor
        public Bord(GameManager manager)
        {
            m_manager = manager;
        }
        #endregion

        #region Public Methods
        public void Initialize()
        {
            m_elementsList = new List<Element>();
            m_deadElementList = new List<Element>();
            BordHeight = 50.0;
            BordWidth = 50.0;
        }
        public void PlayNextDay()
        {
            foreach (Element elem in m_deadElementList)
            {
                m_elementsList.Remove(elem);
            }
            m_deadElementList.Clear();
            Creature crt;
            foreach (Element e in m_elementsList)
            {
                List<Element> privetList = new List<Element>(m_elementsList);
                privetList.Remove(e);
                if (e.EleType == Element.ElementType.Moving)
                {
                    crt = (Creature)e;
                    crt.Think(privetList);
                    float newX = crt.Center.X + crt.Speed * (float)Math.Cos(crt.Direction);
                    float newY = crt.Center.Y + crt.Speed * (float)Math.Sin(crt.Direction);
                    if (newX < -0.5f || newX > 0.5f || newY < -0.5f || newY > 0.5f)
                    {
                        continue;
                    }
                    crt.SetFutureCenterGeometrics(newX,newY,0);
                    if (CheckCrashes(crt, privetList))
                    {
                        continue;
                    }
                    crt.Center = new Vector(newX, newY);
                    crt.UpdateToFutureGeometrics();
                }
            }
        }

        private bool CheckCrashes(Element cur, List<Element> lst)
        {
            foreach (Element e in lst)
            {
                if (MathHelper.CheckSingleIntersection(cur, e))
                {
                    return true;
                }
            }
            return false;
        }



        public void Reset()
        { }
        public void AddElement(Element e)
        {
            m_elementsList.Add(e);
        }
        public List<Element> GetElementList()
        {
            return m_elementsList;
        }

        #endregion

        #region Test
        /*public void PolyPlace()
        {
            int seed = 292;
            Random rand = new Random(seed);
            int nums = 1;// +rand.Next(6);
            for (int i = 0; i < nums; i++)
            {
                m_elementsList.Add(new Shape(rand.Next()));
            }
            Polygon pframe = new Polygon( new List<Vector>{
                new Vector(-0.5,-0.5),
                new Vector(-0.5,0.5),
                new Vector(-0.5,0.5),
                new Vector(0.5,-0.5)
            }) ;
            List<Polygon> lpframe = new List<Polygon>();
            lpframe.Add(pframe);
            Element frame = new Element(lpframe);
            m_elementsList.Add(frame);
        }*/

        public void PolyPlace2()
        {
            int seed = 299;
            Random rand = new Random(seed);
            polyadd(rand.Next(), -0.3, 0.3);
            polyadd(rand.Next(), -0.1, 0.3);
            polyadd(rand.Next(), 0.1, 0.3);
            polyadd(rand.Next(), 0.3, 0.3);

            polyadd(rand.Next(), -0.3, 0.1);
            polyadd(rand.Next(), -0.1, 0.1);
            polyadd(rand.Next(), 0.1, 0.1);
            polyadd(rand.Next(), 0.3, 0.1);

            polyadd(rand.Next(), -0.3, -0.1);
            polyadd(rand.Next(), -0.1, -0.1);
            polyadd(rand.Next(), 0.1, -0.1);
            polyadd(rand.Next(), 0.3, -0.1);

            polyadd(rand.Next(), -0.3, -0.3);
            polyadd(rand.Next(), -0.1, -0.3);
            polyadd(rand.Next(), 0.1, -0.3);
            polyadd(rand.Next(), 0.3, -0.3);

        }

        private void polyadd(int seed, double x, double y)
        {
            Random rand = new Random(seed);
            Polygon p = MathHelper.CreateRandomConvexedPoly(seed++, 0.05);
            Vector v = new Vector(x, y);
            Shape s = new Shape(seed++, p, v);
            s.Direction = 0;
            s.Speed = 0.008f * (float)rand.NextDouble();
            m_elementsList.Add(s);
        }

        #endregion
    }
}
